package user

import (
	"time"

	"gitee.com/yangwencheng/vblog/common"
	"github.com/rs/xid"
)

type Token struct {
	*common.Meta
	Username    string `json:"username" gorm:"column:username" validate:"required"`
	AccessToken string `json:"accessToken" gorm:"column:access_token" validate:"required"`
	ExpiredAt   int64  `json:"expiredAt" gorm:"column:expired_at"`
}

func (*Token) TableName() string {
	return "tokens"
}

func NewToken() *Token {
	return &Token{
		Meta: common.NewMeta(),
	}
}

func (token *Token) GenerateToken() string {
	token.AccessToken = xid.New().String()
	token.ExpiredAt = time.Now().Unix() + 3600*24
	return "tokens"
}

func (token *Token) CheckExpire() error {

	if token.ExpiredAt < time.Now().Unix() {
		return common.NewApiException(common.USER_ERR_CODE, "token已过期")
	}
	return nil
}
